﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AdvanceTimeTableManagement.Manager;

namespace AdvanceTimeTableManagement
{
    public partial class LoginForm : Form
    {
        SessionInfo sessionInfo = null;
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
            {
                MessageBox.Show("Usernamd and password are required!");
                return;
            }

            try
            {
                bool validUser = false;
                using (TimeTableDataContext dataContext = new TimeTableDataContext())
                {
                    var result = from obj in dataContext.UserInfos
                                 where obj.Username == username && obj.Password == password
                                 select new { obj.FirstName, obj.LastName, obj.UserType, obj.UserID };
                    foreach (var item in result)
                    {
                        validUser = true;
                        sessionInfo = new SessionInfo()
                       {
                           UserID = item.UserID,
                           Name = item.FirstName + " " + item.LastName,
                           UserType = item.UserType
                       };
                    }

                    if (!validUser)
                    {
                        MessageBox.Show("Invalid username and password!");
                    }
                    else
                    {
                        AdvanceTimeTableManagement.AdminModule.ClassSetUp formShow = new AdvanceTimeTableManagement.AdminModule.ClassSetUp();
                        formShow.Show();
                        this.Hide();
                    }
                }
            }
            catch
            {
                MessageBox.Show("Error while login");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
